//
//  AppDelegate.swift
//  weibo_yaoye
//
//  Created by mac on 15/5/11.
//  Copyright (c) 2015年 mac. All rights reserved.
//  test

import UIKit

//--------------step <  48 解档获取用户信息  >--------nextsteplocation<<#currentfile#>>---
var shareduserAccount = userAcount.loadUserAccount()
let YYChooseRootVCNotification = "YYChooseRootVCNotification" ///Step: <60>
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        println(shareduserAccount)
        println(NSHomeDirectory())
        println(NSBundle.mainBundle().infoDictionary)
        println(isNewVersion())
        ///Step: <59>
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.makeKeyAndVisible()
        window?.backgroundColor = UIColor.whiteColor()
        showMainInterface()
        //MARK:--------------step<66>---设置风格----------------------------------------
        setbarTint()
        
        ///Step: <60> 通过通知中心的方式实现控制器跳转
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "ChooseRootVC:", name: YYChooseRootVCNotification, object: nil)///Step: <60>
        return true
    }

    //--------------step<56>---通过bundel版本号来判断是否要更新版本----------------------------------------------------------
    func isNewVersion() -> Bool
    {
        //bundle
        let CurrentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //转换浮点数
        let versionValue = NSNumberFormatter().numberFromString(CurrentVersion)!.doubleValue
        //沙盒
        let sandboxKey = "sandboxKey"
        let defaults = NSUserDefaults.standardUserDefaults()
        let sandboxVersion = defaults.doubleForKey(sandboxKey)

        //时刻保证沙盒中版本号和bundle中相同
        defaults.setDouble(versionValue, forKey:"sandboxKey")
      
        println(NSBundle.mainBundle().infoDictionary)
        //比较
        return versionValue > sandboxVersion
    }
    //--------------step<59>---由是否登陆判断跳到何处----------------------------------------------------------
    private func showMainInterface()
    {
        //初始值就是登陆界面
        var sbName = "Main"
        if shareduserAccount != nil
        {
            
            if isNewVersion()
            {//是登陆过的用户 更新版本就显示欢迎界面
            sbName = "NewFeature"
            
            }
            //是登陆过的用户 没有更新版本就进入欢迎页
            sbName = "Welcome"
        }
        //没有登陆就直接进入登陆注册界面
        window?.rootViewController = UIStoryboard.initialViewController(sbName)
        
    }
    
    //--------------step<60>---通过通知中心的方式实现控制器跳转----------------------------------------------------------
    func ChooseRootVC(n: NSNotification)
    {
        let sbName = n.object as! String
        jumbtoNVC(sbName)
        
    }
    func jumbtoNVC(sbName:String) ///Step: <60>
    {
        
        window?.rootViewController = UIStoryboard.initialViewController(sbName)
        
    }
    
    //MARK:--------------step<66>---设置导航拉和tabbar的外观----------------------------------------
    private func setbarTint()
    {
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
    }
    

}

